<?php
/**
 * Created by: zhaoyi (834574377@qq.com)
 * Time: 2023/1/9 17:06
 * Blog: https://www.yuque.com/huangzhenlian
 */

declare(strict_types=1);

namespace App\Application\Api\Task;


use App\Annotation\Api;
use App\Exception\ErrorException;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\DbConnection\Db;
use Hyperf\AsyncQueue\Annotation\AsyncQueueMessage;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;

#[Controller("/api/OrderCancelTask")]
#[Crontab(rule: "*\/5 * * * * *", name: "订单未支付取消", callback: "OrderCancel", memo: "订单为支付取消", enable: "isOrderCancel")]
class OrderCancelTask
{
    //订单状态0未开始，1进行中，2已结束，3已退款，4已取消，5已分账，6待付款
    //订单分账定时更新
    //https://zly.qile168.net/api/OrderCancelTask/OrderCancel
    #[Api]
    #[GetMapping("OrderCancel")]
    public function OrderCancel()
    {
        $where = [];
        $where[] = ['status', '=', 6];
        $where[] = ['created_at', '<', date('Y-m-d H:i:s',time()-120)];
        $res=   Db::table('order')->where($where)->update([
            'status' => 4,
            'updated_at' => date('Y-m-d H:i:s', time())
        ]);
        if ($res) {
            return true;
        } else {
            return false;
        }
    }

    public function isOrderCancel(): bool
    {
        $where = [];
//待支付订单
        $where[] = ['status', '=', 6];
        $where[] = ['created_at', '<', date('Y-m-d H:i:s',time()-120)];
        $order = Db::table('order')->where($where)->select('*')->first();
        if (!empty($order)) {
            return true;
        } else {
            return false;
        }
    }


}